/*
 * @Description: 
 * @Author: 李璕
 * @Date: 2019-08-12 15:37:18
 * @LastEditTime : 2019-12-23 18:10:27
 * @LastEditors  : 熊伟
 */
import Axios from "axios";


const uploadFile = async function (file, type = "file") {
    let userId = this.$store.state.user.userInfo.uid;
    // let tokenRes = await this.api.getUploadFileToken({
    //     user_id: userId
    // });
    let tokenRes = await this.api.upload({ //同步
        user_id: userId,

    }).then(res => {
        console.log(res);

    }).catch(err => {
        console.warn(err.msg)

    });
    if (!(tokenRes && tokenRes.data)) {
        this.$toast.show({
            text: "请稍后重试"
        })
        return false
    }
    let token = tokenRes.data.file_token;
    let param = new FormData();
    param.append("file", file, file.name);
    // param.append("white_sword", token);
    let config = {
        headers: { "Content-Type": "multipart/form-data" },
        unAddAuthorization:true
    };
    // 添加请求头
    let url =  fileServeUrl + "/obs/api/file/file_upload?white_sword=" + token;
    if (type === "image") {
        url =  fileServeUrl + "/obs/api/file/pic_upload?white_sword=" + token;
    }

    param.append('file_token',token);
    let fileRes = await Axios.post(
        url,
        param,
        config,
        { timeout: 60000 } //最大上传时间
    );
    return fileRes

    // if (fileRes.status === 200) {
    //     this.fileList.push({
    //         goods_file: fileRes.data,
    //         fileName: file.name
    //     });
    //     this.successCount++;
    // } else {
    //     this.failCount++;
    // }
    // this.afterUpload(nums, this.successCount, this.failCount);
}




export default uploadFile